package org.zhang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zhang.shortlink.project.dao.entity.LinkBrowserStatsDO;
import org.zhang.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.HashMap;
import java.util.List;

/**
* @author zhang
* @description 针对表【t_link_browser_stats】的数据库操作Mapper
* @createDate 2024-04-25 20:07:27
* @Entity org.zhang.shortlink.project.dao.entity.LinkBrowserStatsDO
*/
@Mapper
public interface LinkBrowserStatsMapper extends BaseMapper<LinkBrowserStatsDO> {

    /**
     * 记录浏览器访问监控数据
     */
    @Insert("INSERT INTO t_link_browser_stats (id, full_short_url, date, cnt, browser, create_time, update_time, del_flag) " +
            "VALUES(#{id}, #{fullShortUrl}, #{date}, #{cnt}, #{browser}, #{createTime},#{updateTime},#{delFlag} ) " +
            "ON DUPLICATE KEY UPDATE cnt = cnt +  #{cnt};")
    void shortLinkBrowserStats(LinkBrowserStatsDO linkBrowserStatsDO);

    /**
     * 根据短链接获取指定日期内浏览器监控数据
     */
    @Select("SELECT " +
            "    browser, " +
            "    SUM(cnt) AS count " +
            "FROM " +
            "    t_link_browser_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, browser;")
    List<HashMap<String, Object>> listBrowserStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

}




